# exception greenlet
from greenlet import greenlet

huge = []


def show_leak():
    def test1():
        gr2.switch()

    def test2():
        huge.extend([x * x for x in range(100)])
        try:
            gr1.switch()
        except Exception:
            print(greenlet.gettrace())
        finally:
            print("finish switch del huge")
            del huge[:]

    gr1 = greenlet(test1)
    gr2 = greenlet(test2)
    gr1.switch()
    # test1已死，再尝试切换将切回其 greenlet的parent。
    gr1 = gr2 = None
    print("length of huge is zero ? {}", len(huge))


if __name__ == '__main__':
    show_leak()
